<% layout('/layouts/default.html', {title: '关联系统用户', libs: ['dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header">
			<div class="box-title">
				<i class="fa icon-notebook"></i> ${text('关联系统用户')}
			</div>
			<div class="box-tools pull-right">
				<a href="#" class="btn btn-default" id="btnSearch" title="${text('查询')}"><i class="fa fa-filter"></i> ${text('查询')}</a>
				<a href="#" class="btn btn-default" id="btnSetting" title="${text('设置')}"><i class="fa fa-navicon"></i></a>
			</div>
		</div>
		<div class="box-body">
			<#form:form id="searchForm" model="${user}" action="${ctx}/flow/user/systemUserList" method="post" class="form-inline hide"
			data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">
				<div class="form-group">
					<label class="control-label">${text('登录名称')}：</label>
					<div class="control-inline">
						<#form:input path="loginCode" maxlength="255" class="form-control width-120"/>
					</div>
				</div>
				<div class="form-group">
					<label class="control-label">${text('手机号码')}：</label>
					<div class="control-inline">
						<#form:input path="mobile" maxlength="255" class="form-control width-120"/>
					</div>
				</div>
				<div class="form-group">
					<button type="submit" class="btn btn-primary btn-sm"><i class="glyphicon glyphicon-search"></i> ${text('查询')}</button>
					<button type="reset" class="btn btn-default btn-sm isQuick"><i class="glyphicon glyphicon-repeat"></i> ${text('重置')}</button>
				</div>
			</#form:form>
			<div class="row">
				<div class="col-xs-10 pr10">
					<table id="dataGrid"></table>
					<div id="dataGridPage"></div>
				</div>
				<div class="col-xs-2 pl0">
					<div id="selectData" class="tags-input"></div>
				</div>
			</div>
		</div>
	</div>
</div>
<% } %>
<script>
	var selectData = "#{isNotBlank(selectData!) ? selectData! : '{}'}",
	selectNum = 0, selectUserCode, dataGrid = $('#dataGrid').dataGrid({
		searchForm: $('#searchForm'),
		columnModel: [
			{header:'${text("登录账号")}', name:'loginCode', width:120, align:"left"},
			{header:'${text("用户名称")}', name:'userName', width:120, align:"left"},
			{header:'${text("电子邮箱")}', name:'email', width:150, align:"center"},
			{header:'${text("手机号码")}', name:'mobile', width:120, align:"center"},
			{header:'${text("用户状态")}', name:'status', width:100, align:"center", frozen:true, formatter: function(val, obj, row, act){
				return js.getDictLabel("#{@DictUtils.getDictListJson('sys_status')}", val, '未知', true);
			}},
			{header:'${text("创建时间")}', name:'createDate', width:180, align:"center"},

			{header:'行数据', name:'rowData', hidden:true, formatter: function(val, obj, row, act){
				return JSON.stringify(row);
			}}
		],
		showCheckbox: '${parameter.checkbox}' == 'true',
		multiboxonly: false, // 单击复选框时再多选
		ajaxSuccess: function(data){
			$.each(selectData, function(key, value){
				dataGrid.dataGrid('setSelectRow', key);
			});
			initSelectTag();
		},
		onSelectRow: function(id, isSelect, event){
			if ('${parameter.checkbox}' == 'true'){
				if(isSelect){
					selectData[id] = JSON.parse(dataGrid.dataGrid('getRowData', id).rowData);
				}else{
					delete selectData[id];
				}
			}else{
				selectData = {};
				selectData[id] = JSON.parse(dataGrid.dataGrid('getRowData', id).rowData);
			}
			initSelectTag();
		},
		onSelectAll: function(ids, isSelect){
			if ('${parameter.checkbox}' == 'true'){
				for (var i=0; i<ids.length; i++){
					if(isSelect){
						selectData[ids[i]] = JSON.parse(dataGrid.dataGrid('getRowData', ids[i]).rowData);
					}else{
						delete selectData[ids[i]];
					}
				}
			}
			initSelectTag();
		},
		ondblClickRow: function(id, rownum, colnum, event){
			if ('${parameter.checkbox}' != 'true'){
				js.layer.$('#' + window.name).closest('.layui-layer')
						.find(".layui-layer-btn0").trigger("click");
			}
			initSelectTag();
		}
	});
	function initSelectTag(){
		selectNum = 0;
		var html = [];
		$.each(selectData, function(key, value){
			selectNum ++;
			html.push('<span class="tag" id="'+key+'_tags-input"><span>'+value.userName+' </span>'
					+ '<a href="#" onclick="removeSelectTag(\''+key+'\');" title="${text("取消选择")}">x</a></span>');
			selectUserCode = value.userCode;
		});
		html.unshift('<div class="title">${text("当前已选择 {0\} 项", "<span id=\"selectNum\">'+selectNum+'</span>")}：</div>');
		$('#selectData').empty().append(html.join(''));
	}
	function removeSelectTag(key){
		delete selectData[key];
		dataGrid.dataGrid('resetSelection', key);
		$('#selectNum').html(--selectNum);
		$('#'+key+'_tags-input').remove();
	}

	var formData = function () {
		return selectUserCode;
	}
</script>